package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

@Service
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Autowired
    private DishMapper dishMapper;
    /**
     * 添加套餐
     * @param setmealDTO
     */
    @CacheEvict(cacheNames = "setmealCache",key = "#setmealDTO.categoryId")
    public void save(SetmealDTO setmealDTO) {
//        向套餐中添加数据
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmealMapper.save(setmeal);
//        套餐菜品表
//        获取生成的id
        Long id = setmeal.getId();
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        for (SetmealDish setmealDish : setmealDishes) {
            setmealDish.setSetmealId(id);
        }
        setmealDishMapper.save(setmealDishes);


    }
    /**
     * 套餐分页查询
     * @return
     */
    @Override
    public PageResult page(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());
        Page<SetmealVO> page = setmealMapper.page(setmealPageQueryDTO);
        return new PageResult(page.getTotal(),page.getResult());
    }
    /**
     * 批量删除套餐
     * @param ids
     */
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    @Override
    public void deleteById(List<Long> ids) {
//        起售中的套餐不能删除
        for (Long id : ids) {
          Setmeal setmeal =   setmealMapper.getById(id);
          if(setmeal.getStatus() == StatusConstant.ENABLE){
              throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
          }
        }
//        删除套餐中的数据
        setmealMapper.deleteById(ids);

//        删除套餐和菜品相关联的表中数据
        setmealDishMapper.deleteBySetmealId(ids);
    }
    /**
     * 根据id查询套餐
     */
    @Override
    public SetmealVO getById(Long id) {
        Setmeal setmeal = setmealMapper.getById(id);
        SetmealVO setmealVO = new SetmealVO();
        BeanUtils.copyProperties(setmeal,setmealVO);
        List<SetmealDish> setmealDishes = setmealDishMapper.getBySetmealId(id);
        setmealVO.setSetmealDishes(setmealDishes);
        return setmealVO;
    }
    /**
     * 修改套餐
     * @param setmealDTO
     * @return
     */
    @Override
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)

    public void update(SetmealDTO setmealDTO) {
//        更新套餐中的数据
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmealMapper.update(setmeal);
//        更新套餐与菜品相关联的数据
        List<Long> id = Collections.singletonList(setmealDTO.getId());
        setmealDishMapper.deleteBySetmealId(id);
//        查询是否有
        Long ids = setmeal.getId();
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        for (SetmealDish setmealDish : setmealDishes) {
            setmealDish.setSetmealId(ids);
        }
        setmealDishMapper.save(setmealDishes);

    }
    /**
     * 启用禁用套餐
     * @param id
     * @param status
     * @return
     */
    @Override
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)

    public void startOrStop(Long id, Integer status) {
//        起售套餐时，如果套餐内包含停售的菜品，则不能起售
        if(StatusConstant.ENABLE == status){
            List<SetmealDish> setmealDishes = setmealDishMapper.getBySetmealId(id);
            for (SetmealDish setmealDish : setmealDishes) {
                Long dishId = setmealDish.getDishId();
                Dish dish = dishMapper.queryById(dishId);
                if(dish.getStatus() == StatusConstant.DISABLE){
                    throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ENABLE_FAILED);
                }
            }
        }
        Setmeal setmeal = setmealMapper.getById(id);
        setmeal.setStatus(status);
        setmealMapper.update(setmeal);

    }

    /**
     * 条件查询
     * @param setmeal
     * @return
     */
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.list(setmeal);
        return list;
    }

    /**
     * 根据id查询菜品选项
     * @param id
     * @return
     */
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }
}
